package com.cqtd.user.action;

import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqtd.base.pojo.FooGenericSearch;
import com.cqtd.base.pojo.FooGenericSearchResult;
import com.cqtd.base.service.FooGenericService;
import com.cqtd.user.model.SystemUser;
import com.cqtd.util.FooUtils;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/user/SystemUserAction")
public class SystemUserAction {
	private static final Logger logger = Logger.getLogger(SystemUserAction.class);
	@Autowired
	private FooGenericService fooGenericService;
	
	/*
	 * 用户列表
	 */
	@RequestMapping("/userlist")
	@ResponseBody
	public void userlist(HttpServletResponse response) {

		String hql = "from SystemUser";
		String countHql = "select count(id) " + hql;

		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		fooGenericSearch.setPage(1);
		fooGenericSearch.setRows(15);
		fooGenericSearch.setCountHql(countHql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService
				.searchAndCount(fooGenericSearch);
		Map<String, Object> map = Maps.newHashMap();
		map.put("datas", fooGenericSearchResult.getResult());
		map.put("total", fooGenericSearchResult.getTotalCount());
		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 用户账号编辑
	 */
	@RequestMapping("/usereditor")
	@ResponseBody
	public void editor(HttpServletResponse response, SystemUser systemUser) {
		fooGenericService.save(systemUser);
		try {
			FooUtils.printJsonObject(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 用户账号激活与失效
	 */
	@RequestMapping("/useractive")
	@ResponseBody
	public void remove(HttpServletResponse response, @RequestParam(value="id",required=false)String id,@RequestParam(value="accountStatus",required=false)String accountStatus) {
		SystemUser systemUser = (SystemUser)fooGenericService.get(SystemUser.class, id);
		
		if(systemUser!=null && !"".equals(accountStatus)){
			systemUser.setAccountStatus(accountStatus);
			fooGenericService.save(systemUser);
		}else{
			logger.info("================no result can be fond======");
		}
		try{
			FooUtils.printJsonSuccessMsg(response);
		}catch (Exception e) {
			logger.error(e.getMessage());
		}
		
	}

	/*
	 * 用户账号删除
	 */
	@RequestMapping("/userremove")
	@ResponseBody
	public void remove(HttpServletResponse response, @RequestParam(value="id",required=false)String id) {
		SystemUser systemUser = (SystemUser)fooGenericService.get(SystemUser.class, id);
		if(systemUser!=null){
			fooGenericService.delete(systemUser);
		}else{
			logger.info("================no result can be deleted======");
		}
		try{
			FooUtils.printJsonSuccessMsg(response);
		}catch (Exception e) {
			logger.error(e.getMessage());
		}
		
	}
}
